// order//order/skuManage/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDialog: 0,//1父规格2子规格
    showDelDialog: 0,//1删除父规格及下属规格
    point: '',
    pointDelFather: '',//删除规格提示语
    pointDelChild: '',
    inputContent: '',
    inputPrice: '',//规格价格
    skulist: [],
    tagid: 0,
    optionId: 0
  },

  // 获取规格列表
  getSkulist: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Product/GetOptionList?uid=' + app.globalData.userInfo.Uid + '&tagId=0',
      success: function (res) {
        if (res.data.Code == 0) {
          let list = res.data.Data;
          if (list.length > 0) {
            that.setData({
              skulist: list
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSkulist();
  },

  addSku: function () {
    this.setData({
      showDialog: 1,
      tagid: 0
    })
  },
  addChildSku: function (e) {
    let title = '“' + e.currentTarget.dataset.title + '”下属新规格';
    let tagid = e.currentTarget.dataset.tagid;
    this.setData({
      showDialog: 2,
      point: title,
      tagid: tagid
    })
  },
  // 添加规格属性
  addSkuConfirm: function () {
    let tagid = this.data.tagid;
    let name = this.data.inputContent;
    if (name == '') {
      wx.showToast({
        title: '请填写规格',
        duration: 1500
      })
      return;
    }
    wx.showLoading({
      title: '正在保存',
      mask: true,
    })
    if (tagid > 0) {//添加子规格
      this.addOption(tagid);
    } else {//添加父规格
      this.addOptionGroup();
    }
  },

  isMultiple: function(e){
    let isMultiple = e.currentTarget.dataset.type;
    let tagid = e.currentTarget.dataset.tagid;
    this.addOptionGroup(isMultiple,tagid);
  },

  addOptionGroup: function (isMultiple = 0,tagid = 0) {
    let that = this;
    let name = that.data.inputContent;
    wx.request({
      url: app.globalData.host + '/api/Product/AddOptionGroup?uid=' + app.globalData.userInfo.Uid + '&name=' + name + '&isMultiple=' + isMultiple + '&tagId=' + tagid,
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          that.closedialog();
          let titletip='添加成功';
          if(tagid>0){
            titletip='保存成功'
          }
          wx.showToast({
            title: titletip,
            duration: 1500
          })
          that.getSkulist();
          that.setData({
            inputContent: ''
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 添加子规格
  addOption: function (tagid) {
    let that = this;
    let name = that.data.inputContent;
    let price = that.data.inputPrice;
    if(price == ''){
      price = 0;
    }
    wx.request({
      url: app.globalData.host + '/api/Product/AddOption?tagId=' + parseInt(tagid) + '&uid=' + app.globalData.userInfo.Uid + '&name=' + name + '&price=' + price,
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          that.closedialog();
          wx.showToast({
            title: '添加成功',
            duration: 1500
          })
          that.getSkulist();
          that.setData({
            inputContent: ''
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 删除规格
  delSkuConfirm: function () {
    let optionId = this.data.optionId;
    if (optionId > 0) {//删除子规格
      this.delOption(optionId);
    } else {//删除父规格及下属规格
      this.delOptionGroup();
    }
  },

  delOption: function (optionId) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Product/DeleteOption?optionId=' + optionId + '&uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          that.closedialog();
          wx.showToast({
            title: '删除成功',
            duration: 1500
          })
          that.getSkulist();
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  delOptionGroup: function () {
    let that = this;
    let tagid = that.data.tagid;
    wx.request({
      url: app.globalData.host + '/api/Product/DeleteOptionGroup?tagId=' + tagid,
      success: function (res) {
        if (res.data.Code == 0) {
          that.closedialog();
          wx.showToast({
            title: '删除成功',
            duration: 1500
          })
          that.getSkulist();
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  
  closedialog: function () {
    this.setData({
      showDialog: 0,
      showDelDialog: 0
    })
  },
  delFatherSku: function (e) {
    let title = e.currentTarget.dataset.title;
    let tagid = e.currentTarget.dataset.tagid;
    let point = '是否删除“' + title + '”规格，删除后“' + title + '”下属规格将全部删除！确定是否进行此操作?';
    this.setData({
      showDelDialog: 1,
      pointDelFather: point,
      tagid: tagid,
      optionId: 0
    })
  },
  delChildSku: function (e) {
    let title = e.currentTarget.dataset.title;
    let optionid = e.currentTarget.dataset.optionid;
    let point = '您确定删除“' + title + '”?\n删除后将无法恢复';
    this.setData({
      showDelDialog: 2,
      pointDelChild: point,
      tagid: 0,
      optionId: optionid
    })
  },
  bindInput: function (e) {
    this.setData({
      inputContent: e.detail.value
    })
  },
  bindInputPrice:  function (e) {
    this.setData({
      inputPrice: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})